<template>
  <div style="width: 100%; height: 100%; position: relative">
    <div :id="idname" style="width: 100%; height: 100%" v-if="!isShow"></div>
    <div class="no_data" v-show="isShow">
      <img ref="img" src="@/assets/images/no_data.png" alt="" />
      <p>暂无数据</p>
    </div>
  </div>
</template>

<script>
import echarts from "echarts";
import { EleResize } from "@/utils/esresize.js";
export default {
  props: {
    idname: {
      type: String,
      requied: true,
    },
    datalist: {
      type: Object,
      default: {},
    },
  },
  data() {
    return {
      isShow: false,
    };
  },
  watch: {
    datalist: {
      handler(val, oldVal) {
        var newarr = val.data.filter((item) => item.value != 0);
        if (newarr && newarr.length) {
          this.$nextTick(() => {
            this.chartColumn(1);
          });
        } else {
          this.$nextTick(() => {
            this.chartColumn(2);
          });
        }
      },
      deep: true,
    },
  },
  methods: {
    // 只适用图表方法
    drawLine(dom, option) {
      if (document.getElementById(dom)) {
        let myChart = echarts.init(document.getElementById(dom));
        let resizeDiv = document.getElementById(dom);
        myChart.clear();
        myChart.setOption(option);
        let listener = function () {
          myChart.resize();
        };
        EleResize.on(resizeDiv, listener);
      }
    },
    chartColumn(type) {
      let dom = this.idname;
      var rich = {
        name: {
          color: "#333",
          fontSize: 14,
          padding: [0, 5],
          fontWeight: "400",
          align: "right",
        },
        percent: {
          color: "#fff",
          align: "right",
          fontSize: 14,
          fontWeight: "500",
          padding: [0, 5],
        },
        hr: {
          borderColor: "#333",
          width: "100%",
          borderWidth: 1,
          height: 0,
        },
      };
      let option1 = {
        graphic: [
          {
            type: "image",
            id: "logo",
            left: "center",
            top: "50",
            bounding: "raw",
            style: {
              image: this.$refs["img"].currentSrc,
              width: 92,
              opacity: 1,
            },
          },
        ],
      };
      let option2 = {
        backgroundColor: "#fff",
        tooltip: {
          trigger: "item",
          position: ["50%", "50%"],
          formatter: "{a} <br/>{b}: {c}",
        },
        grid: {
          top: 0,
        },
        series: [
          {
            name: this.datalist.series.name,
            type: "pie",
            radius: [0, "70%"],
            color: "#49dbc4",
            labelLine: {
              normal: {
                lineStyle: {
                  color: "#333",
                },
              },
            },
            label: {
              normal: {
                formatter: (params) => {
                  return "{name|" + params.name + "}";
                },
                rich: rich,
              },
            },
            data: this.datalist.data,
          },
          {
            name: this.datalist.series.name,
            type: "pie",
            color: "#49dbc4",
            radius: [0, "70%"],
            label: {
              normal: {
                position: "inner",
                formatter: (params) => {
                  return "{percent|" + params.percent.toFixed(0) + "%}";
                },
                rich: rich,
              },
            },
            itemStyle: {
              normal: {
                borderColor: "#fff",
                borderWidth: 5,
              },
              emphasis: {
                borderWidth: 0,
                shadowBlur: 5,
                shadowOffsetX: 0,
                shadowColor: "rgba(0, 0, 0, 0.5)",
              },
            },
            data: this.datalist.data,
          },
        ],
      };
      let option = type == 1 ? option2 : option1;
      this.drawLine(dom, option);
    },
  },
  mounted() {
    this.chartColumn();
  },
  created() {},
};
</script>

<style></style>
